package com.lyh.db137.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lyh.db137.common.JwtUtils;
import com.lyh.db137.common.R;
import com.lyh.db137.entity.Menu;
import com.lyh.db137.entity.User;
import com.lyh.db137.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 想要告诉你
 * @CreateTime: 2024/12/24 11:50
 * @Description:
 * @Version: 1.0
 */

@RestController
@CrossOrigin(origins = "*")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 根据登录用户返回用户的菜单集合数据
     * @return
     */
    @RequestMapping("/selectUserMenu")
    public List<Menu> selectUserMenu(int id) {
        return userService.selectUserByName(id);
    }

    /*
    @RequestMapping("/login")
    public User login(String username, String password) {
        List<User> list = userService.list();
        for (User user : list) {
            System.out.println(user.getUname() + " " + user.getUpwd());
            if (user.getUname().equals(username) && user.getUpwd().equals(password)) {
                return user;
            }
        }
        return null;
    }
    */

    @RequestMapping("/loginJwt")
    public R loginJwt(@RequestBody User user) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(user);
        User user1 = userService.getOne(queryWrapper);
        if (user1 != null) {//登录成功
            //生成token
            Map<String, Object> map = new HashMap<>();
            map.put("userid", user1.getId());
            String jwtToken = JwtUtils.createJwtToken(map);
            return new R().addData("token", jwtToken);
        }
        return new R().setCode(500).setMessage("登录失败!");
    }

    @RequestMapping("/register")
    public String insertUser(User user) {
        return userService.save(user) ? "注册成功" : "注册失败";
    }

    @RequestMapping("/selectUser")
    public R selectUser() {
        List<User> users = userService.list();
        return new R().addData("users", users);
    }

    @RequestMapping("/selectUserById")
    public R selectUserById(@RequestHeader("token") String token) {
        Integer userid = JwtUtils.getJwtToken(token, "userid");

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userid);
        List<User> users = userService.list(queryWrapper);
        return new R().addData("users", users);
    }

    @RequestMapping("/selectUserByUname")
    public R selectUserByUname(String uname) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uname", uname);
        List<User> users = userService.list(queryWrapper);
        return new R().addData("users", users);
    }

    @RequestMapping("/addUser")
    public R addUser(User user) {
        return userService.save(user) ? new R().setCode(200).setMessage("添加成功") : new R().setCode(500).setMessage("添加失败");
    }

    @RequestMapping("/deleteUser")
    public R deleteUser(int id){
        return userService.removeById(id) ? new R().setCode(200).setMessage("删除成功") : new R().setCode(500).setMessage("删除失败");
    }

    @RequestMapping("/updateUser")
    public R updateUser(User user){
        return userService.updateById(user) ? new R().setCode(200).setMessage("修改成功") : new R().setCode(500).setMessage("修改失败");
    }
}
